package com.duleme.servlet;

import com.duleme.common.Config;
import com.duleme.common.EncodeUtils;
import com.duleme.entity.Order;
import com.duleme.entity.User;
import com.duleme.service.ShopService;
import com.duleme.service.impl.ShopServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PayOrderServlet",urlPatterns = "/pay/order")
public class PayOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        User user = (User) request.getSession().getAttribute("user");
        ShopService shopService = new ShopServiceImpl();
        List<Integer> list = shopService.getOrderIDByUser(user.getUserId(), Config.placeit);
        Boolean flag= true;
        for (Integer i:list){
            Order order = shopService.getOrderByID(i);
            order.setOrderState(Config.payit);
            flag&=shopService.setOrderState(order);
        }
        if (flag){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("2");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
